package cn.com.ddapp.ddm.ui.mine;

import android.content.Intent;
import android.graphics.Color;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.OnClick;
import cn.com.ddapp.ddm.R;
import cn.com.ddapp.ddm.adapter.PayGridViewAdapter;
import cn.com.ddapp.ddm.base.BaseActivity;
import cn.com.ddapp.ddm.base.BaseApplication;
import cn.com.ddapp.ddm.bean.mine.MyGridView;

public class PayActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView back;
    @BindView(R.id.titletext)
    TextView titletext;
    @BindView(R.id.gv_pay_price)
    MyGridView gvPrice;
    @BindView(R.id.et_pay_other_price)
    EditText etPayOtherPrice;
    @BindView(R.id.btn_pay_charge)
    Button btnCharge;
    @BindView(R.id.pay_exL)
    LinearLayout payExL;

    String getPrice;
    private ImageView ivBack;
    private List<String> list ;
    int orientation;

    @Override
    public void initView() {
        titletext.setText(R.string.pay);
        list = new ArrayList<>();
        list.add("10元");
        list.add("50元");
        list.add("100元");
        list.add("500元");
        list.add("1000元");
        list.add("1500元");
        list.add("2000元");
        list.add("2500元");
        list.add("3000元");
        //判断是否横屏,1竖屏0横屏
        orientation = getResources().getConfiguration().orientation;
        if (orientation == 0 || orientation == 2) {
            DisplayMetrics dm = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(dm);
            int screenHeigh = dm.heightPixels;
            ViewGroup.LayoutParams params = btnCharge.getLayoutParams();
            params.height = screenHeigh / 14;
            btnCharge.setLayoutParams(params);
            payExL.setLayoutParams(params);
        }
        gvPrice.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //将所有的item颜色变回默认的
                for (int i = 0; i < list.size(); i++) {
                    gvPrice.getChildAt(i).setBackgroundColor(Color.WHITE);
                    TextView price = (TextView) gvPrice.getChildAt(i).findViewById(R.id.tv_price);
                    price.setTextColor(0xFFFF6F01);
                }
                //设置选中的item的背景颜色和字体颜色
                gvPrice.getChildAt(position).setBackgroundColor(0xFFFF6F01);
                TextView price = (TextView) gvPrice.getChildAt(position).findViewById(R.id.tv_price);
                price.setTextColor(Color.WHITE);
                etPayOtherPrice.setText("");
                String a = list.get(position);
                getPrice = a.substring(0, a.length() - 1);
            }
        });
        //加载gridview
        final PayGridViewAdapter adapter = new PayGridViewAdapter(this, list);
        gvPrice.setAdapter(adapter);

        etPayOtherPrice.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                //控制开头不为0
                String text = editable.toString();
                int len = text.length();
                if (len==1&&text.equals("0")){
                    editable.clear();
                }
            }
        });
    }

    /**
     * 初始化金额
     */
    @Override
    public void initData() {

    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_pay;
    }

    @Override
    protected String getFragmentTag() {
        return null;
    }

    @OnClick({R.id.back, R.id.btn_pay_charge, R.id.et_pay_other_price})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back://返回
                finish();
                break;
            case R.id.et_pay_other_price:
                //将所有的item颜色变回默认的
                for (int i = 0; i < list.size(); i++) {
                    gvPrice.getChildAt(i).setBackgroundColor(Color.WHITE);
                    TextView price = (TextView) gvPrice.getChildAt(i).findViewById(R.id.tv_price);
                    price.setTextColor(0xFFFF6F01);
                }
                getPrice = null;
                break;

            case R.id.btn_pay_charge://充值
                String etPrice = etPayOtherPrice.getText().toString();
                String price;
                if ("".equals(etPrice) && getPrice == null) {
                    BaseApplication.showToast("请选择充值多少", Toast.LENGTH_SHORT, 0, Gravity.TOP);
                } else if ("".equals(etPrice) && "".equals(getPrice)) {
                    BaseApplication.showToast("请选择充值多少", Toast.LENGTH_SHORT, 0, Gravity.TOP);
                } else {
                    if ("".equals(etPrice)) {
                        price = getPrice;
                    } else {
                        price = etPrice;
                    }
                    Intent intent = new Intent(this,PayMoneyActivity.class);
                    intent.putExtra("money",price);
                    startActivity(intent);
                    //BaseApplication.getIntstance().showToast(price, Toast.LENGTH_SHORT, 0, Gravity.TOP);
                    finish();
                }
                break;
        }
    }
}
